<script setup lang="ts">
import { onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { showSuccessToast, showFailToast } from 'vant'
import router from '@/router'
import { get } from '@/utils/service'
import { SESSION_LAST_CODE, SESSION_MEMBER_INFO } from '@/utils/baseConfig'
const route = useRoute()
const code = (route.query.code as string) || ''
const scope = route.query.scope as string
const selfCode = localStorage.getItem(SESSION_LAST_CODE)
if (code == null || code == '' || code == undefined) {
  showSuccessToast('静默获取微信公众号信息 失败，请退出页面重新进入')
}
onMounted(() => {
  try {
    onLogin()
  } catch (error) {
    console.log('onMounted error:' + error)
  }
})
function onLogin() {
  get('/memberWechatNew/onWechatLogin', {
    code: code,
    selfCode: selfCode,
    scope: scope,
  })
    .then((result) => {
      if (result.code == 0) {
        showSuccessToast('静默获取用户信息成功')
        localStorage.setItem(SESSION_MEMBER_INFO + selfCode, JSON.stringify(result.data))
        router.push({
          path: '/?code=' + selfCode,
        })
      } else {
        showFailToast('静默获取用户信息失败' + result.msg)
      }
    })
    .catch((err) => {
      console.log(err)
    })
}
</script>
<template>
  <div style="text-align: center">静默获取用户信息</div>
</template>
